const gulp = require('gulp'),
       htmlmin = require("gulp-htmlmin"),
       minifyCss = require("gulp-clean-css"),
       uglify = require("gulp-uglify"),
       babel = require("gulp-babel")
       connect = require("gulp-connect") ;

//压缩CSS
gulp.task( "css",()=>{
    gulp.src("src/css/*.css")
        .pipe(minifyCss() )
        .pipe( gulp.dest("dist/css"))
        .pipe(connect.reload() );
} );

//html
gulp.task( "html",()=>{
    gulp.src("src/**/*.html")
        .pipe(htmlmin( {collapseWhitespace: true} ))
        .pipe(gulp.dest("dist"))
        .pipe(connect.reload());
} );

//js
gulp.task("js", ()=>{
    gulp.src("src/js/**/*.js")
        .pipe( babel({
            presets:['env']
        }) )
        .pipe(uglify() )
        .pipe(gulp.dest("dist/js"))
        .pipe(connect.reload());
} );

//copy
gulp.task("copy-images",()=>{
    gulp.src("src/images/**/*.*")
        .pipe(gulp.dest("dist/images"));
} );
gulp.task("copy-lib",()=>{
    gulp.src("src/lib/**/*.*")
        .pipe(gulp.dest("dist/lib"));
} );

gulp.task("copy", ["copy-images", "copy-lib"]);

//webserver
gulp.task("webserver", ()=>{
    connect.server({
        root:'src',
        livereload:true
    });
} );

//监视任务
gulp.task("watch",()=>{
    gulp.watch("src/css/*.css", ["css"]);
    gulp.watch("src/js/*.js", ["js"]);
    gulp.watch("src/**/*.html", ["html"]);
} )

//设置默认任务
gulp.task('default', ["css","html", "js", "copy", "webserver", "watch" ] );